﻿using System;
using System.IO;

using InputFileGenerator;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace InputFileGeneratorTest
{
    /// <summary>
    /// Summary description for GraphTraversalFileCreationTest
    /// </summary>
    [TestClass]
    public class GraphTraversalFileCreationTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        //                               {0}          {1}            {2}                            {3}                                  {4}     {5}
        private string[] smallArgs =  { "-name", "graphtraversal", "-file", "C:\\DWARFS2\\Bin\\InputFileGenerator\\smallContent.txt", "-size", "1000" };
        private string[] mediumArgs = { "-name", "graphtraversal", "-file", "C:\\DWARFS2\\Bin\\InputFileGenerator\\mediumContent.txt", "-size", "2000" };
        private string[] largeArgs = { "-name", "graphtraversal", "-file", "C:\\DWARFS2\\Bin\\InputFileGenerator\\largeContent.txt", "-size", "4000" };

        private bool IsFileCareful(string[] args)
        {
            if (!File.Exists(args[3]))
            {
                return false;
            }

            return IsFormatCareful(args);

        }

        private bool IsFormatCareful(string[] args)
        {   
            FileStream stream = null;
            StreamReader reader = null;
            bool returnval = false;

            try
            {
                stream = new FileStream(args[3], FileMode.Open, FileAccess.Read, FileShare.Read);
                reader = new StreamReader(stream);
                int size = int.Parse(reader.ReadLine());
                int expectedSize = int.Parse(args[5]);

                if (size != expectedSize)
                {
                    throw new Exception("Incorrect graph size.");
                }

                for (int i = 0; i < size; i++)
                {
                    string[] str = reader.ReadLine().Split(":".ToCharArray());

                    if (int.Parse(str[0]) != i)
                    {
                        throw new Exception("Incorrect graph structure.");
                    }   

                    //check the edge of graph
                    if (!MatrixHelper.IsMatrixCareful(reader, size, 1, TypeElement.INT, " ", StrictSize.NO))
                    {
                        throw new Exception("Incorrect graph structure.");
                    }
                }

                returnval =  true;
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return returnval;
        }

        /// <summary>
        ///A test Graph Traversal generation process
        ///</summary>
        [TestMethod()]
        [DeploymentItem("InputFileGenerator.exe")]
        public void MainTest()
        {
            string[] args = smallArgs;

            //test for small 
            Program_Accessor.Main(args);
            Assert.IsTrue(IsFileCareful(args));

            args = mediumArgs;

            //test for medium 
            Program_Accessor.Main(args);
            Assert.IsTrue(IsFileCareful(args));

            args = largeArgs;

            //test for large 
            Program_Accessor.Main(args);
            Assert.IsTrue(IsFileCareful(args));
        }
    }
}
